/***************************
NO.01 根据输入的mtrl_cate,query bis_bom,bis_mtrl .返回mtrl_id,mtrl_mkr
NO.02 增加wh_out_prd_qty记录内部订单出货数量，OPE_EFF_FLG作为WO是否原箱返回的标识
NO.03 返回内容增加 path_id_fk,path_ver_fk,str_ope_no;
NO.04 返回内容增加 RET_WO表所有内容;
NO.05 领料时增加rl_prd_qty数量 而不是增加pln_prd_qty
/***************************/
package com.service;

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.tx.Xpcrtno.XpcrtnoI;
import com.tx.Xpcrtno.XpcrtnoO;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import com.dao.IBisBomDao;
import com.dao.IBisDataDao;
import com.dao.IBisMdlDefDao;
import com.dao.IBisMtrlDao;
import com.dao.IBisPathiDao;
import com.dao.IRetBoxDao;
import com.dao.IRetPrdInDao;
import com.dao.IRetShipNoticeDao;
import com.dao.IRetWODao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_bom;
import com.model.Bis_data;
import com.model.Bis_mdl_def;
import com.model.Bis_mtrl;
import com.model.Bis_pathi;
import com.model.Bis_pathiId;
import com.model.Ret_box;
import com.model.Ret_prd_in;
import com.model.Ret_ship_notice;
import com.model.Ret_wo;
import com.tx.Xpaplyso.XpaplysoI;
import com.tx.Xpaplyso.XpaplysoIA;
import com.tx.Xpaplyso.XpaplysoO;
import com.tx.Xpaplywo.XpaplywoI;
import com.tx.Xpaplywo.XpaplywoIA;
import com.tx.Xpaplywo.XpaplywoO;
import com.tx.Xpaplywo.XpaplywoOA;
import com.tx.Xpaplywo.XpaplywoOB;
import com.util.MqOpe;
import com.util.Util;

import static com.def.GenericDef.*;

@Scope("prototype")
@Service("XpaplywoService")
public class XpaplywoService extends ICIMBaseService{
	private Logger logger = Logger.getLogger(XpaplywoService.class);
	XpaplywoI inTrx;
	XpaplywoO outTrx;
	private String gszMsgBuf = "";	
	@Autowired
	private IRetWODao retWODao;
	@Autowired
	private IBisMdlDefDao bisMdlDefDao;
	@Autowired
	private IBisBomDao bisBomDao;
	@Autowired
	private IBisMtrlDao bisMtrlDao;
	@Autowired
	private IRetBoxDao retBoxDao;
	@Autowired
	private IRetShipNoticeDao retShipNoticeDao;
	@Autowired
	private IRetPrdInDao retPrdInDao;
	@Autowired
	private IBisPathiDao bisPathIDao;
	@Autowired
	XpaplysoService xpaplysoService;
	@Autowired
	private IBisDataDao bisDataDao;
	@Autowired
	private XpcrtnoService xpcrtnoService;
	
	/******宏定义******/
	public final static String WO_TYPE_WORDER = "W";
	private static final String OPEN_WO = "Y";
	private static final String CLOSE_WO = "X";

	public String subMainProc(String strInMsg) {
		MqOpe mqOpe1 = new MqOpe();
		String reply = "";
		Session session = retWODao.getCrSession();
		inTrx = new XpaplywoI();
		outTrx = new XpaplywoO();
		try {
			inTrx = (XpaplywoI) mqOpe1.inStr2Obj(strInMsg, inTrx);
			logger.info("InTrx is " + strInMsg);
			outTrx.setTrx_id("XPAPLYWO");
			outTrx.setType_id("O");
			String actionFlg = inTrx.getAction_flg();
			if (actionFlg.equals("Q")) {
				getWoProc();
			} else if (actionFlg.equals("T")) {//出货根据DN查询WO
				getWoListByDN();
			} else if (actionFlg.equals("D")) {
				outTrx = delWoProc(inTrx);
			} else if (actionFlg.equals("U")) {
				 updWoProc(inTrx);
			} else if (actionFlg.equals("A")) {
				addWoProc(inTrx);
			} else if (actionFlg.equals("W")) {
				outTrx = getAllWoProc(inTrx);
			} else if (actionFlg.equals("N")) {//仓库收料界面 自动创建内部订单时使用
				AddOrUpdateWorder();
			} else if (actionFlg.equals("S")) {
				outTrx = calculateWorderCnt(inTrx);
			} else if (actionFlg.equals(CLOSE_WO)) {
				closeWOProc();
			} else if (actionFlg.equals(OPEN_WO)) {
				openWOProc();
			} else if (actionFlg.equals("I")) {//获取已经绑定SO的wo
				getWoBindSoProc();
			} else if(actionFlg.equals("L")){
				getWoInfoByOpeCateTyp();
			}else {
				errActionFnc();
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				session.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			logger.error(Util.stackTraceToString(ex));
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());

		} finally {
			reply = mqOpe1.obj2Str(outTrx);
			logger.info(reply);

		}
		return reply;

	}
/**
 * 根据站点的mdl_cate与内部订单的ndl_cate,筛选出可以使用的内部订单。
 */
	private void getWoInfoByOpeCateTyp() {
		try {
			List<XpaplywoOA> oaryList = new ArrayList<XpaplywoOA>();
			XpaplywoIA iary = inTrx.getIary().get(0);
			String wo_cate="",wo_stat="",ope_id="",
					ope_ver="",mdl_cate="";
			wo_cate = iary.getWo_cate();
			wo_stat = iary.getWo_stat();
			ope_id = iary.getOpe_id();
			ope_ver = iary.getOpe_ver();
			String sql = "SELECT * FROM BIS_DATA A JOIN BIS_OPE B " +
								"ON B.PROC_ID = A.DATA_ID WHERE  B.OPE_ID ='"+ope_id+"'" +
								"AND B.OPE_VER='"+ope_ver+"'AND A.DATA_CATE IN('PRST')";
			List<Bis_data> bis_data_list = bisDataDao.findBySQL(sql, Bis_data.class);
			if(bis_data_list.size()==0){
				//此站点没有制程代码或制程代码不存在
				outTrx.setRtn_code(E_XPAPLYWO_OTHER_ERROR + "");
				outTrx.setRtn_mesg("此站点["+ope_id+"]的没有设置制程代码，或者对应的制程代码没有维护!");
				return;
			}
			mdl_cate = bis_data_list.get(0).getExt_4();
			
			String wo_sql = "FROM Ret_wo where wo_typ='W' ";
			if (wo_cate != null) {
				wo_sql += " AND wo_cate='" + wo_cate + "'";
			}
			if (wo_stat != null) {
				wo_sql += " AND wo_stat='" + wo_stat + "'";
			}
			/*if (!mdl_cate.equals(_SPACE)) {
				if (("JB-01".equals(ope_id)) || ("JB-02".equals(ope_id)) || ("JB-03".equals(ope_id))) {
					wo_sql += "AND (mdl_cate_fk like '%" + mdl_cate + "%' OR mdl_cate_fk in ('B','BC'))";
				}else {
					wo_sql += " AND mdl_cate_fk like'%" + mdl_cate + "%'";
				}
			}*/
			
			wo_sql += " ORDER BY WO_ID DESC";
			List<Ret_wo> woList = retWODao.find(wo_sql);
			if (woList != null) {
				for (Ret_wo ret_wo : woList) {
					XpaplywoOA oary = new XpaplywoOA();
					oary.setWo_id(ret_wo.getWo_id());
					oary.setWo_cate(ret_wo.getWo_cate());
					if (ret_wo.getPln_stb_date() != null) {
						oary.setPln_stb_date(ret_wo.getPln_stb_date().toString().substring(0, 10));
					}
					if (ret_wo.getPln_cmp_date() != null) {
						oary.setPln_cmp_date(ret_wo.getPln_cmp_date().toString().substring(0, 10));
					}
					oary.setWo_prty(ret_wo.getWo_prty());
					oary.setDest_shop(ret_wo.getDest_shop());
					oary.setMdl_id(ret_wo.getMdl_id_fk());
					oary.setPln_prd_qty(ret_wo.getPln_prd_qty());
					oary.setWo_dsc(ret_wo.getWo_dsc());
					oary.setSo_id(ret_wo.getSo_id());
					oary.setVcr_flg(ret_wo.getVcr_flg_fk());
					oary.setOpe_eff_flg(ret_wo.getOpe_eff_flg());
					oary.setSwh_cnt_flg(ret_wo.getSwh_cnt_flg());
					oary.setEvt_usr(ret_wo.getEvt_usr());
					oary.setTo_thickness(ret_wo.getTo_thickness());
					oary.setFrom_thickness(ret_wo.getFrom_thickness());
					oary.setMdl_cate_fk(ret_wo.getMdl_cate_fk());
					oary.setCus_id_fk(ret_wo.getCus_id_fk());
					if (ret_wo.getEvt_timestamp() != null) {
						oary.setEvt_timestamp(ret_wo.getEvt_timestamp().toString());
					}
					oaryList.add(oary);
				}
			}
			outTrx.setOary(oaryList);
			outTrx.setTbl_cnt(oaryList.size());
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setRtn_mesg(RETURN_MESG_OK);
		}catch (Exception ex) {
			outTrx.setRtn_code(E_XPAPLYWO_OTHER_ERROR + "");
			outTrx.setRtn_mesg(ex.getMessage());
			logger.error(Util.stackTraceToString(ex));
			return;
		}
	}

	private void getWoListByDN() {
		outTrx.setTrx_id("XPAPLYWO");
		outTrx.setType_id("O");
		try {
			XpaplywoIA iary = inTrx.getIary().get(0);
			String dn_no = iary.getDn_no();
			String datein = iary.getDate();
			String timein = iary.getTime();
			Date date = java.sql.Date.valueOf(datein);
			Time time = java.sql.Time.valueOf(timein);
			if (Util.isSpaceCheck(dn_no)) {
				outTrx.setRtn_mesg("查询条件不能为空！");
				outTrx.setRtn_code(E_XPAPLYWO_QUERY_CONDITION_NULL + "");
				return;
			}
			List<Ret_wo> oary_wo = new ArrayList<Ret_wo>();
			String sql = "FROM Ret_ship_notice where dn_stat <> 'COMP' and dn_no='" + dn_no + "' and ship_date='" + date + "' and ship_time='" + time + "'";
			List<Ret_ship_notice> retShipNoticeList = retShipNoticeDao.find(sql);
			for (Ret_ship_notice dn : retShipNoticeList) {
				Ret_wo wo = new Ret_wo();
				wo = retWODao.get(dn.getWo_id_fk());
				oary_wo.add(wo);
			}

			outTrx.setTbl_cnt(oary_wo.size());
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setRtn_mesg(RETURN_MESG_OK);
			outTrx.setOary_wo(oary_wo);
		} catch (Exception ex) {
			outTrx.setRtn_code(E_XPAPLYWO_OTHER_ERROR + "");
			outTrx.setRtn_mesg(ex.getMessage());
			logger.error(Util.stackTraceToString(ex));
			return;
		}
	}

	public void getWoProc() {
		outTrx.setTrx_id("XPAPLYWO");
		outTrx.setType_id("O");
		XpaplywoIA iary = inTrx.getIary().get(0);
		String wo_id = iary.getWo_id();
		String wo_dsc = iary.getWo_dsc();
		String wo_prty = iary.getWo_prty();
		String mdl_id = iary.getMdl_id();
		String dest_shop = iary.getDest_shop();
		String mtrl_cate = iary.getMtrl_cate();
		String wo_cate = iary.getWo_cate();
		String wo_stat = iary.getWo_stat();
		String wo_stats = iary.getWo_stats();
		String cus_id = iary.getCus_id();
		String mtrl_prod_id_fk = iary.getMtrl_prod_id_fk();
		String wo_typ = iary.getWo_typ();
		String so_id = inTrx.getSo_id();

		
		String sql = "FROM Ret_wo where 1=1 ";
		if (wo_typ != null){
			sql = sql + "AND wo_typ = '" + wo_typ + "'";
		}else {
			sql = sql + "AND wo_typ = 'W'";
		}
		if (so_id != null){
			sql = sql + "AND so_id = '" + so_id + "'";
		}
		if (wo_id != null) {
			sql += " AND wo_id='" + wo_id + "'";
		} else {
			if (wo_dsc != null) {
				sql += " AND wo_dsc like " + wo_dsc + "'%'";
			}
			if (wo_prty != null) {
				sql += " AND wo_prty='" + wo_prty + "'";
			}
			if (mdl_id != null) {
				sql += " AND mdl_id_fk='" + mdl_id + "'";
			}
			if (dest_shop != null) {
				sql += " AND dest_shop='" + dest_shop + "'";
			}
			if (wo_cate != null) {
				sql += " AND wo_cate='" + wo_cate + "'";
			}
			if (wo_stat != null) {
				sql += " AND wo_stat='" + wo_stat + "'";
			}
			if (wo_stats != null) {
				sql += " AND wo_stat in (" + wo_stats + ")";
			}
			if (cus_id != null) {
				sql += " AND cus_id_fk='" + cus_id + "'";
			}
			if (mtrl_prod_id_fk != null) {
				sql += " AND mtrl_prod_id_fk='" + mtrl_prod_id_fk + "'";
			}
//			if("Y".equals(iary.getDps_flg())){
//				if("A".equals(iary.getDps_mdl_cate())){
//					sql += " AND ( rsv_dps_qty = 0 or rsv_dps_qty < rsv_pln_prd_qty )";
//				}else if("B".equals(iary.getDps_mdl_cate())){
//					sql += " AND ( rsv_snd_dps_qty = 0 or rsv_snd_dps_qty < rsv_pln_prd_qty )";
//				}else if("C".equals(iary.getDps_mdl_cate())){
//					sql += " AND ( rsv_trd_dps_qty = 0 or rsv_trd_dps_qty < rsv_pln_prd_qty )";
//				}
//			}
			sql += " ORDER BY WO_ID DESC";
		}
		List<Ret_wo> woList = retWODao.find(sql);
		List<XpaplywoOA> oaryList = new ArrayList<XpaplywoOA>();
		if (woList != null) {
			for (Ret_wo ret_wo : woList) {
				XpaplywoOA oary = new XpaplywoOA();
				
				if("Y".equals(iary.getDps_flg())){
					List<Ret_prd_in> ret_prd_inList = new ArrayList<Ret_prd_in>();
					int cntWO = 0;
					String hqlPrdIn = " FROM Ret_prd_in where  ppbox_stat IN ( 'WFRL','RELS','EMPT' ) and wo_id_fk = '"+ret_wo.getWo_id()+"'";
					ret_prd_inList = retPrdInDao.find(hqlPrdIn);
					if (!ret_prd_inList.isEmpty()){
						for (Ret_prd_in item : ret_prd_inList) {
							cntWO += item.getPrd_tl_qty();
						}
					}
					oary.setOut_4200_qty(cntWO);
				}
				oary.setWo_id(ret_wo.getWo_id());
				oary.setWo_cate(ret_wo.getWo_cate());
				Ret_wo  retWo1=retWODao.get(ret_wo.getSo_id());
				if(retWo1!=null){
					if ("Y".equals(retWo1.getOpe_eff_flg())){
						oary.setOldBox("原箱原位回");
					}else if("Y".equals(retWo1.getVcr_flg_fk())){
						oary.setOldBox("原箱回");
					}else{
						oary.setOldBox("无要求");
					}
				}
           if(!Util.isSpaceCheck(ret_wo.getWo_cate())){
			   String hql4 = "From Bis_data where data_cate ='WOAB'";
			   hql4 += " and data_id = '" + ret_wo.getWo_cate() + "'";
			   List<Bis_data> bisDataList = bisDataDao.find(hql4);
			   if(bisDataList.size()>0){
				   String woCateDsc = bisDataList.get(0).getData_desc();
				   oary.setWoCateDsc(woCateDsc);
			   }
		   }

				if(!Util.isSpaceCheck(ret_wo.getMdl_id_fk())){
					Bis_mdl_def bismdldef = bisMdlDefDao.get(ret_wo.getMdl_id_fk());
					oary.setMtrl_size(bismdldef.getMdl_size());
				}
				if (ret_wo.getPln_stb_date() != null) {
					oary.setPln_stb_date(ret_wo.getPln_stb_date().toString().substring(0, 10));
				}
				if (ret_wo.getPln_cmp_date() != null) {
					oary.setPln_cmp_date(ret_wo.getPln_cmp_date().toString().substring(0, 10));
				}
				oary.setWo_stat(getStatDsc(ret_wo.getWo_stat()));
				oary.setWo_prty(ret_wo.getWo_prty());
				oary.setDest_shop(ret_wo.getDest_shop());
				oary.setMtrl_prod_id_fk(ret_wo.getMtrl_prod_id_fk());
				oary.setMdl_id(ret_wo.getMdl_id_fk());
				oary.setPln_prd_qty(ret_wo.getPln_prd_qty());
				oary.setRsv_dn_qty(ret_wo.getRsv_dn_qty());
				oary.setWo_dsc(ret_wo.getWo_dsc());
				oary.setMainT(ret_wo.getMaint());
				oary.setShtT(ret_wo.getShtt());
				oary.setJgyq(ret_wo.getJgyq());
				oary.setFhdir(ret_wo.getFhdir());
				oary.setSo_id(ret_wo.getSo_id());
				oary.setVcr_flg(ret_wo.getVcr_flg_fk());
				oary.setOpe_eff_flg(ret_wo.getOpe_eff_flg());
				oary.setSwh_cnt_flg(ret_wo.getSwh_cnt_flg());
				oary.setWip_wh_in_prd_qty(ret_wo.getWip_wh_in_prd_qty());
				oary.setEvt_usr(ret_wo.getEvt_usr());
				if (ret_wo.getMdl_id_fk() != null) {
					Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(ret_wo.getMdl_id_fk());
					if (bis_mdl_def != null) {
						oary.setMdl_dsc(bis_mdl_def.getMdl_dsc());
						oary.setLayout_id_fk(bis_mdl_def.getLayot_id_fk());
						//oary.setMdl_guige(bis_mdl_def.getMdl_typ()+"*"+ret_wo.getFrom_thickness()+"mm");
						oary.setMdl_guige(bis_mdl_def.getMdl_typ());
						String rsvInfo = bis_mdl_def.getRsv_info();
						oary.setDqr_flg(rsvInfo != null && rsvInfo.charAt(1) == 'Y' ? "Y" : "N");
					}
				}
				if (ret_wo.getEvt_timestamp() != null) {
					oary.setEvt_timestamp(ret_wo.getEvt_timestamp().toString());
				}
				if (mtrl_cate != null && ret_wo.getMdl_id_fk() != null) {
					String hql = "FROM Bis_bom where id.mdl_id_fk='" + ret_wo.getMdl_id_fk() + "'";
					List<Bis_bom> bisBomList = bisBomDao.find(hql);
					for (Bis_bom bis_bom : bisBomList) {
						String mtrl_prod_id = bis_bom.getId().getMtrl_prod_id_fk();
						Bis_mtrl bis_mtrl = bisMtrlDao.get(mtrl_prod_id);
						if (bis_mtrl != null && mtrl_cate.equals(bis_mtrl.getMtrl_cate())) {
							oary.setMtrl_prod_id(mtrl_prod_id);
							oary.setMtrl_mkr(bis_mtrl.getMtrl_mkr());
						}
					}
				}
				oary.setPath_id_fk(ret_wo.getPath_id_fk());//NO.03
				oary.setPath_ver_fk(ret_wo.getPath_ver_fk());//NO.03
				oary.setStr_ope_no(ret_wo.getStr_ope_no());//NO.03
				/******
				 * NO.04 增加返回值 ret_wo表剩下的所有值
				 * ******/
				oary.setWo_typ(ret_wo.getWo_typ());
				oary.setErp_bom_id(ret_wo.getErp_bom_id());
				oary.setErp_path_id(ret_wo.getErp_path_id());
				oary.setErp_path_ver(ret_wo.getErp_path_ver());
				oary.setPln_prd_qty(ret_wo.getPln_prd_qty());
				oary.setRl_prd_qty(ret_wo.getRl_prd_qty());
				oary.setComp_prd_qty(ret_wo.getComp_prd_qty());
				oary.setWh_in_prd_qty(ret_wo.getWh_in_prd_qty());
				oary.setWh_out_prd_qty(ret_wo.getWh_out_prd_qty());
				oary.setWip_wh_in_prd_qty(ret_wo.getWip_wh_in_prd_qty());
				oary.setScrp_prd_qty(ret_wo.getScrp_prd_qty());
				oary.setPlant_id(ret_wo.getPlant_id());
				oary.setScrp_prd_qty(ret_wo.getScrp_prd_qty());
				oary.setPlant_id(ret_wo.getPlant_id());
				oary.setOk_ratio(ret_wo.getOk_ratio());
				oary.setFrom_thickness(ret_wo.getFrom_thickness());
				oary.setTo_thickness(ret_wo.getTo_thickness());
				oary.setMdl_cate_fk(ret_wo.getMdl_cate_fk());
				if(ret_wo.getT_thickness() != null){
					oary.setT_thickness(ret_wo.getT_thickness().toString());
				}
				if(ret_wo.getC_thickness() != null){
					oary.setC_thickness(ret_wo.getC_thickness().toString());
				}
				oary.setWo_note(ret_wo.getWo_note());
				oary.setTh_mdl_id_fk(ret_wo.getTh_mdl_id_fk());
				oary.setFm_mdl_id_fk(ret_wo.getFm_mdl_id_fk());
				oary.setCut_mdl_id_fk(ret_wo.getCut_mdl_id_fk());

				oary.setAct_stb_timestamp(ret_wo.getAct_stb_timestamp());
				oary.setAct_cmp_timestamp(ret_wo.getAct_cmp_timestamp());
				oary.setCrt_usr(ret_wo.getCrt_usr());
				oary.setCrt_timestamp(ret_wo.getCrt_timestamp());
				oary.setCus_id_fk(ret_wo.getCus_id_fk());
				
				oary.setPln_stb_timestamp(ret_wo.getPln_stb_timestamp());
				oary.setPln_cmp_timestamp(ret_wo.getPln_cmp_timestamp());
				oary.setRsv_pln_prd_qty(ret_wo.getRsv_pln_prd_qty());
				oary.setRsv_dps_qty(ret_wo.getRsv_dps_qty());
				oary.setRemain_dps_qty(ret_wo.getRsv_pln_prd_qty()-ret_wo.getRsv_dps_qty());
				oary.setRsv_snd_dps_qty(ret_wo.getRsv_snd_dps_qty());
				oary.setRemain_snd_dps_qty(ret_wo.getRsv_pln_prd_qty()- ret_wo.getRsv_snd_dps_qty());
				oary.setRsv_trd_dps_qty(ret_wo.getRsv_trd_dps_qty());
				oary.setRemain_trd_dps_qty(ret_wo.getRsv_pln_prd_qty()-ret_wo.getRsv_trd_dps_qty());
				oary.setAct_dps_qty(ret_wo.getAct_dps_qty());
				oary.setAct_snd_dps_qty(ret_wo.getAct_snd_dps_qty());
				oary.setAct_trd_dps_qty(ret_wo.getAct_trd_dps_qty());
				oary.setLayoutId(ret_wo.getLayoutId());
	            oary.setSpec(ret_wo.getSpec());
				if(_YES.equals(inTrx.getIary().get(0).getSum_cnt())){
					oary.setWait_box_sum(retPrdInDao.getWaitBoxSumByWo(ret_wo.getWo_id()));
				}
				oary.setCus_info_snd(ret_wo.getCus_info_snd());
				oary.setCus_info_fst(ret_wo.getCus_info_fst());
				if (!Util.isSpaceCheck(inTrx.getGet_first_ope())) {
					String path_id = ret_wo.getPath_id_fk();
					String path_ver = ret_wo.getPath_ver_fk();
					
					Bis_pathiId bis_pathiId = new Bis_pathiId();
					Bis_pathi bis_pathi = new Bis_pathi();
					bis_pathiId.setPath_id(path_id);
					bis_pathiId.setPath_ver(path_ver);
					
					bis_pathi = bisPathIDao.get(bis_pathiId);
					
					if (bis_pathi != null) {
						oary.setFirst_ope_id(bis_pathi.getFirst_ope_no());
					}
					
				}
				oaryList.add(oary);
				
			}
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
	}

	public void addWoProc(XpaplywoI xpaplywoI) {
		outTrx.setTrx_id("XPAPLYWO");
		outTrx.setType_id("O");
		long tbl_cnt = xpaplywoI.getTbl_cnt();
		for (int i = 0; i < tbl_cnt; i++) {
			Ret_wo ret_wo = new Ret_wo();
			XpaplywoIA iary = xpaplywoI.getIary().get(i);
			/*if (retWODao.get(iary.getWo_id()) != null) {
				outTrx.setRtn_code(E_XPAPLYWO_WORDER_ALREADY_EXIST + "");
				outTrx.setRtn_mesg("此工单ID已经存在,错误！！");
				return ;
			}*/

			long lRc = _NORMAL;
			MqOpe mqOpe = new MqOpe();
			String woId ="Y";
			String key_id =""; //年月日 171217
			String yyyymmnn = Util.getEvtTimeforVer();
			key_id += yyyymmnn;
			woId += key_id;
			XpcrtnoI xpcrtNOI = new XpcrtnoI();
			xpcrtNOI.setType("XPCRTNO");
			xpcrtNOI.setTrx_id("I");
			xpcrtNOI.setKey_id(key_id);
			xpcrtNOI.setType(STRING_SERIAL_TYPE_WO);
			xpcrtNOI.setAction_flg("L");
			String strInMsg = mqOpe.obj2Str(xpcrtNOI);
			String strOutMsg = xpcrtnoService.subMainProc(strInMsg);
			XpcrtnoO xpcrtNoO = new XpcrtnoO();
			xpcrtNoO = (XpcrtnoO) mqOpe.inStr2Obj(strOutMsg, xpcrtNoO);
			if (!xpcrtNoO.getRtn_code().equals(RETURN_CODE_OK)) {
				gszMsgBuf = xpcrtNoO.getRtn_mesg();
			} else {
				woId = woId + xpcrtNoO.getSerial_no();
			}
			iary.setWo_id(woId);

			ret_wo.setWo_id(woId);
			ret_wo.setWo_typ(WO_TYPE_WORDER);
			ret_wo.setWo_prty(iary.getWo_prty());
			ret_wo.setWo_dsc(iary.getWo_dsc());
			ret_wo.setMaint(iary.getMainT());
			ret_wo.setShtt(iary.getShtT());
			ret_wo.setJgyq(iary.getJgyq());
			ret_wo.setFhdir(iary.getFhdir());
			ret_wo.setWo_cate(iary.getWo_cate());
			ret_wo.setWo_prty(iary.getWo_prty());

			ret_wo.setCus_id_fk(iary.getCus_id());
			ret_wo.setRsv_pln_prd_qty(iary.getRsv_pln_prd_qty());

			ret_wo.setPln_cmp_timestamp(iary.getPln_cmp_timestamp());
			ret_wo.setPln_stb_timestamp(iary.getPln_stb_timestamp());
			ret_wo.setPln_stb_date(Date.valueOf(iary.getPln_stb_timestamp().toString().substring(0, 10)));
			ret_wo.setPln_cmp_date(Date.valueOf(iary.getPln_cmp_timestamp().toString().substring(0, 10)));

			ret_wo.setDest_shop(iary.getDest_shop());

			ret_wo.setEvt_usr(iary.getEvt_user());
			ret_wo.setEvt_timestamp(Util.getCurrentTimestamp());
			ret_wo.setMtrl_prod_id_fk(iary.getMtrl_prod_id_fk());
			
			ret_wo.setPln_prd_qty(0);
			ret_wo.setRl_prd_qty(0);
			ret_wo.setComp_prd_qty(0);
			ret_wo.setWh_in_prd_qty(0);
			ret_wo.setScrp_prd_qty(0);
			ret_wo.setWip_wh_in_prd_qty(0);
			ret_wo.setWh_out_prd_qty(0);
			ret_wo.setRsv_dps_qty(0);
			ret_wo.setBnd_box_qty(0);
			ret_wo.setRsv_dn_qty(0);
			ret_wo.setAct_dps_qty(0);
			ret_wo.setWip_wh_out_prd_qty(0);
			ret_wo.setRsv_snd_dps_qty(0);
			ret_wo.setAct_snd_dps_qty(0);
			ret_wo.setRsv_trd_dps_qty(0);
			ret_wo.setAct_trd_dps_qty(0);
			ret_wo.setRsv_fth_dps_qty(0);
			ret_wo.setAct_fth_dps_qty(0);
			
			ret_wo.setVcr_flg_fk(iary.getVcr_flg());
			ret_wo.setSwh_cnt_flg(iary.getSwh_cnt_flg());
			ret_wo.setOpe_eff_flg(iary.getOpe_eff_flg());
			ret_wo.setWo_stat("WAIT");
			ret_wo.setPlant_id("TODO");//TODO
			ret_wo.setEvt_usr(iary.getEvt_user());
			ret_wo.setCrt_timestamp(Util.getCurrentTimestamp());
			ret_wo.setCrt_usr(iary.getEvt_user());
			ret_wo.setWh_rwk_qty(0);
			ret_wo.setCus_rma_qty(0);
			ret_wo.setCus_info_fst("");
			ret_wo.setCus_info_snd("");
			ret_wo.setPlant_id(iary.getPlant_id());
			ret_wo.setLayoutId(iary.getLayoutId());
			ret_wo.setSpec(iary.getSpec());
			retWODao.save(ret_wo);
		}
		getWoProc();
	}

	public XpaplywoO delWoProc(XpaplywoI xpaplywoI) {
		XpaplywoO xpaplywoO = new XpaplywoO();

		xpaplywoO.setTrx_id("XPAPLYWO");
		xpaplywoO.setType_id("O");
		try {
			for (XpaplywoIA iary : xpaplywoI.getIary()) {
				String woId = iary.getWo_id();
				Ret_wo ret_wo = retWODao.get(woId);
				String hql = "FROM Ret_prd_in where wo_id_fk = ?";
				List<Ret_prd_in> retPrdInList = retPrdInDao.list(hql,woId);
				if(!retPrdInList.isEmpty()){
					xpaplywoO.setRtn_code(E_XPAPLYWO_OTHER_ERROR + "");
					xpaplywoO.setRtn_mesg("此内部订单已有绑定了来料数据,不能被删除");
					return xpaplywoO;
				}
				if (ret_wo.getSo_id() != null) {
					String soId = ret_wo.getSo_id();
					Ret_wo ret_so  = retWODao.getWithLock(soId, LockMode.UPGRADE); 
					ret_so.setRl_prd_qty(ret_so.getRl_prd_qty() - ret_wo.getRsv_pln_prd_qty());
					retWODao.update(ret_so);
				}
				retWODao.delete(ret_wo);
			}
		} catch (Exception ex) {
			xpaplywoO.setRtn_code(E_XPAPLYWO_OTHER_ERROR + "");
			xpaplywoO.setRtn_mesg(ex.toString());
			logger.error(Util.stackTraceToString(ex));
			return xpaplywoO;
		}
		xpaplywoO.setRtn_code(RETURN_CODE_OK);
		xpaplywoO.setRtn_mesg(RETURN_MESG_OK);
		return xpaplywoO;
	}

	public void updWoProc(XpaplywoI xpaplywoI) {
		MqOpe mqOpe = new MqOpe();
		outTrx.setTrx_id("XPAPLYWO");
		outTrx.setType_id("O");
		long tbl_cnt = xpaplywoI.getTbl_cnt();
		try {
			for (int i = 0; i < tbl_cnt; i++) {

				XpaplywoIA iary = xpaplywoI.getIary().get(i);
				Ret_wo ret_wo = retWODao.get(iary.getWo_id());
				String hql = "FROM Ret_prd_in where wo_id_fk = ?";
				List<Ret_prd_in> retPrdInList = retPrdInDao.list(hql,iary.getWo_id());
				if (iary.getSo_id() != null) {
					if (!iary.getSo_id().equals(ret_wo.getSo_id())) {
						XpaplysoI xpaplysoI = new XpaplysoI();
						xpaplysoI.setTrx_id("XPAPLYSO");
						xpaplysoI.setAction_flg("B");
						XpaplysoIA xpaplysoIa = new XpaplysoIA();
						xpaplysoIa.setWo_id(iary.getWo_id());
						xpaplysoIa.setSo_id(iary.getSo_id());
						List<XpaplysoIA> iaryList = new ArrayList<XpaplysoIA>();
						iaryList.add(xpaplysoIa);
						xpaplysoI.setIary(iaryList);
						xpaplysoI.setTbl_cnt(1);
						String inMsg = mqOpe.obj2Str(xpaplysoI);
						String outMsg = xpaplysoService.subMainProc(inMsg);

						XpaplysoO xpaplysoO = new XpaplysoO();
						xpaplysoO = (XpaplysoO) mqOpe.inStr2Obj(outMsg, xpaplysoO);
						if (!xpaplysoO.getRtn_code().equals(RETURN_CODE_OK)) {
							outTrx.setRtn_code(E_XPAPLYSO_MDL_DEF_NOT_FOUND + "");
							outTrx.setRtn_mesg(outTrx.getRtn_mesg());
							return ;
						}
					}
				}
				if (iary.getWo_prty() != null) {
					ret_wo.setWo_prty(iary.getWo_prty());
				}
				if (iary.getWo_dsc() != null) {
					ret_wo.setWo_dsc(iary.getWo_dsc());
				}
				if (iary.getMainT() != null) {
					ret_wo.setMaint(iary.getMainT());
				}
				if (iary.getShtT() != null) {
					ret_wo.setShtt(iary.getShtT());
				}
				if (iary.getJgyq() != null) {
					ret_wo.setJgyq(iary.getJgyq());
				}
				if (iary.getFhdir() != null) {
					ret_wo.setFhdir(iary.getFhdir());
				}
				if (iary.getWo_cate() != null) {
					ret_wo.setWo_cate(iary.getWo_cate());
				}
				if (iary.getWo_prty() != null) {
					ret_wo.setWo_prty(iary.getWo_prty());
				}
				if (iary.getDest_shop() != null) {
					ret_wo.setDest_shop(iary.getDest_shop());
				}
				if(!Util.isSpaceCheck(ret_wo.getSo_id())){
					Ret_wo ret_so = retWODao.get(ret_wo.getSo_id());
					int diffcnt = iary.getRsv_pln_prd_qty() - ret_wo.getRsv_pln_prd_qty();
					ret_so.setRl_prd_qty(ret_so.getRl_prd_qty() + diffcnt);
					ret_so.setPln_prd_qty(ret_so.getPln_prd_qty() + diffcnt);
					ret_so.setWo_cate(iary.getWo_cate());
					retWODao.update(ret_so);
				}
				if (ret_wo.getPln_prd_qty() > iary.getRsv_pln_prd_qty()){
					outTrx.setRtn_code(E_XPAPLYWO_QTY_CONSTRAINT + "");
					gszMsgBuf = "修改的预计来料数量: [" + iary.getRsv_pln_prd_qty().toString() + 
							"] 小于实际来料数量: [" +ret_wo.getPln_prd_qty()+"]";
					outTrx.setRtn_mesg(gszMsgBuf);
					return ;
					
				}
				ret_wo.setRsv_pln_prd_qty(iary.getRsv_pln_prd_qty());
				ret_wo.setPln_cmp_timestamp(iary.getPln_cmp_timestamp());
				ret_wo.setPln_stb_timestamp(iary.getPln_stb_timestamp());
				ret_wo.setPln_stb_date(Date.valueOf(iary.getPln_stb_timestamp().toString().substring(0, 10)));
				ret_wo.setPln_cmp_date(Date.valueOf(iary.getPln_cmp_timestamp().toString().substring(0, 10)));

				if (iary.getPln_prd_qty() != null) {
					ret_wo.setPln_prd_qty(Integer.parseInt(iary.getPln_prd_qty()));
				}
				if (iary.getVcr_flg() != null) {
					ret_wo.setVcr_flg_fk(iary.getVcr_flg());
				}
				if (iary.getSwh_cnt_flg() != null) {
					ret_wo.setSwh_cnt_flg(iary.getSwh_cnt_flg());
				}
				if (iary.getOpe_eff_flg() != null) {
					ret_wo.setOpe_eff_flg(iary.getOpe_eff_flg());
				}
				ret_wo.setEvt_usr(iary.getEvt_user());
				if (iary.getPlant_id()!= null) {
					ret_wo.setPlant_id(iary.getPlant_id());
				}else{
					ret_wo.setPlant_id("TODO");// TODO
				}
				ret_wo.setEvt_timestamp(Util.getCurrentTimestamp());
				if (iary.getMdl_id() != null) {
					ret_wo.setMdl_id_fk(iary.getMdl_id());
					String mdl_id = iary.getMdl_id();
					Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(mdl_id);
					if (bis_mdl_def == null) {
						outTrx.setRtn_code(E_XPAPLYSO_MDL_DEF_NOT_FOUND + "");
						outTrx.setRtn_mesg("找不到产品[" + mdl_id + "]");
						return ;
					}
				}
               if(!Util.isSpaceCheck(iary.getSpec())){
				   ret_wo.setSpec(iary.getSpec());
			   }
			   if(!Util.isSpaceCheck(iary.getLayoutId())){
				   ret_wo.setLayoutId(iary.getLayoutId());
			   }
				String mtrlProdId = iary.getMtrl_prod_id_fk();
				ret_wo.setMtrl_prod_id_fk(mtrlProdId);
				retWODao.save(ret_wo);
				for(Ret_prd_in retPrdIn : retPrdInList){
					retPrdIn.setMtrl_prod_id_fk(mtrlProdId);
					retPrdIn.setEvt_cate(EVT_MTRL_UPDATE);//定义来料料号变更时的EVT_CATE
					retPrdInDao.update(retPrdIn);
				}
				
			}
		} catch (Exception ex) {
			outTrx.setRtn_code(E_XPAPLYWO_OTHER_ERROR + "");
			outTrx.setRtn_mesg(ex.getMessage());
			return ;
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	public XpaplywoO getAllWoProc(XpaplywoI xpaplywoI) {
		XpaplywoO xpaplywoO = new XpaplywoO();
		String sql = "FROM Ret_wo where wo_typ='W' ORDER BY WO_ID DESC";
		List<Ret_wo> woList = retWODao.find(sql);
		List<XpaplywoOA> oaryList = new ArrayList<XpaplywoOA>();
		if (woList != null) {
			for (Ret_wo ret_wo : woList) {
				XpaplywoOA oary = new XpaplywoOA();
				oary.setWo_id(ret_wo.getWo_id());
				oary.setWo_cate(ret_wo.getWo_cate());
				if (ret_wo.getPln_stb_date() != null) {
					oary.setPln_stb_date(ret_wo.getPln_stb_date().toString().substring(0, 10));
				}
				if (ret_wo.getPln_cmp_date() != null) {
					oary.setPln_cmp_date(ret_wo.getPln_cmp_date().toString().substring(0, 10));
				}
				oary.setWo_prty(ret_wo.getWo_prty());
				oary.setDest_shop(ret_wo.getDest_shop());
				oary.setMdl_id(ret_wo.getMdl_id_fk());
				oary.setPln_prd_qty(ret_wo.getPln_prd_qty());
				oary.setWo_dsc(ret_wo.getWo_dsc());
				oary.setSo_id(ret_wo.getSo_id());
				oary.setVcr_flg(ret_wo.getVcr_flg_fk());
				oary.setOpe_eff_flg(ret_wo.getOpe_eff_flg());
				oary.setSwh_cnt_flg(ret_wo.getSwh_cnt_flg());
				oary.setEvt_usr(ret_wo.getEvt_usr());
				oary.setCus_id_fk(ret_wo.getCus_id_fk());
				oary.setTh_mdl_id_fk(ret_wo.getTh_mdl_id_fk());
				oary.setFm_mdl_id_fk(ret_wo.getFm_mdl_id_fk());
				oary.setCut_mdl_id_fk(ret_wo.getCut_mdl_id_fk());
				if (ret_wo.getEvt_timestamp() != null) {
					oary.setEvt_timestamp(ret_wo.getEvt_timestamp().toString());
				}
				oaryList.add(oary);
			}
		}
		xpaplywoO.setRtn_code(RETURN_CODE_OK);
		xpaplywoO.setRtn_mesg(RETURN_MESG_OK);
		xpaplywoO.setOary(oaryList);
		xpaplywoO.setTbl_cnt(oaryList.size());

		return xpaplywoO;
	}

	public void AddOrUpdateWorder() {

		outTrx.setTrx_id("XPAPLYWO");
		outTrx.setType_id("O");
		long tbl_cnt = inTrx.getTbl_cnt();
		for (int i = 0; i < tbl_cnt; i++) {
			XpaplywoIA iary = inTrx.getIary().get(i);
			String woId = iary.getWo_id();
			Ret_wo ret_wo = retWODao.getWithLock(woId, LockMode.UPGRADE);
			if (ret_wo == null) {
				ret_wo = new Ret_wo();
				ret_wo.setWo_id(woId);
				ret_wo.setWo_typ(WO_TYPE_WORDER);
				ret_wo.setWo_prty(iary.getWo_prty());
				ret_wo.setWo_dsc(iary.getWo_dsc());
				ret_wo.setWo_cate(iary.getWo_cate());
				ret_wo.setWo_prty(iary.getWo_prty());
				ret_wo.setDest_shop(iary.getDest_shop());
				ret_wo.setMtrl_prod_id_fk(iary.getMtrl_prod_id_fk());
				if (iary.getPln_stb_date() != null) {
					ret_wo.setPln_stb_date(Date.valueOf(iary.getPln_stb_date()));
				}
				if (iary.getPln_cmp_date() != null) {
					ret_wo.setPln_cmp_date(Date.valueOf(iary.getPln_cmp_date()));
				}
				ret_wo.setRl_prd_qty(0);
				ret_wo.setComp_prd_qty(0);
				ret_wo.setWh_in_prd_qty(0);
				ret_wo.setScrp_prd_qty(0);
				ret_wo.setWip_wh_in_prd_qty(0);
				ret_wo.setWh_out_prd_qty(0);
				ret_wo.setVcr_flg_fk(iary.getVcr_flg());
				ret_wo.setSwh_cnt_flg(iary.getSwh_cnt_flg());
				ret_wo.setWip_wh_in_prd_qty(0);
				ret_wo.setRsv_snd_dps_qty(0);
				ret_wo.setAct_snd_dps_qty(0);
				ret_wo.setRsv_trd_dps_qty(0);
				ret_wo.setAct_trd_dps_qty(0);
				ret_wo.setRsv_fth_dps_qty(0);
				ret_wo.setAct_fth_dps_qty(0);
				ret_wo.setPln_prd_qty(0);
				ret_wo.setBnd_box_qty(0);
				ret_wo.setWo_stat("WAIT");
				ret_wo.setPlant_id("TODO");//TODO
				ret_wo.setCrt_timestamp(Util.getCurrentTimestamp());
				ret_wo.setCrt_usr(iary.getEvt_user());
			}

			if (iary.getPln_prd_qty() != null) {
				int old_pnl_prd_qty = 0;
				int enlarge_pnl_prd_qty = Integer.valueOf(iary.getPln_prd_qty());
				if (ret_wo.getPln_prd_qty() == null) {
					ret_wo.setPln_prd_qty(enlarge_pnl_prd_qty);
				} else {
					old_pnl_prd_qty = ret_wo.getPln_prd_qty();
					ret_wo.setPln_prd_qty(old_pnl_prd_qty + enlarge_pnl_prd_qty);
				}
				if(inTrx.getAct_flg().equals("A")){
					if(Util.isSpaceCheck(ret_wo.getBnd_box_flg())){
						ret_wo.setBnd_box_flg(_YES);
					}
					if(Util.isSpaceCheck(ret_wo.getBnd_fst_box_usr())){
						ret_wo.setBnd_fst_box_usr(iary.getEvt_user());
					}
					if(ret_wo.getBnd_box_timestamp() == null){
						ret_wo.setBnd_box_timestamp(Util.getCurrentTimestamp());
					}
					ret_wo.setBnd_box_qty(ret_wo.getBnd_box_qty() + 1);
				}else if(inTrx.getAct_flg().equals("D")){
					int bnd_box_qty = ret_wo.getBnd_box_qty() - 1;
					if(bnd_box_qty == 0){
						ret_wo.setBnd_box_flg(null);
						ret_wo.setBnd_box_timestamp(null);
						ret_wo.setBnd_fst_box_usr(null);
					}
					ret_wo.setBnd_box_qty(bnd_box_qty);
				}
			}
			ret_wo.setEvt_usr(iary.getEvt_user());
			ret_wo.setEvt_timestamp(Util.getCurrentTimestamp());
			if (WO_STATUS_CLOS.equals(ret_wo.getWo_stat())) {
				outTrx.setRtn_code(E_XPAPLYSO_CLOSE_STAT + "");
				outTrx.setRtn_mesg("内部订单已经被关闭,不可再做发料,请去5200界面重新打开此内部订单");
				return;
			}

			retWODao.saveOrUpdate(ret_wo);
			

		}
		getWoProc();//NO.04
		//NO.04		xpaplywoO.setRtn_code(RETURN_CODE_OK);
		//NO.04		xpaplywoO.setRtn_mesg(RETURN_MESG_OK);

	}

	public XpaplywoO calculateWorderCnt(XpaplywoI xpaplywoI) {
		XpaplywoO xpaplywoO = new XpaplywoO();
		XpaplywoOB oaryB = new XpaplywoOB();

		XpaplywoIA iary = xpaplywoI.getIary().get(0);
		String wo_id = iary.getWo_id();
		Ret_wo ret_wo = retWODao.get(wo_id);

		long wip_qty = 0;
		long sale_qty = 0;
		long pln_prd_qty = 0;
		String hql = "FROM Ret_box where box_stat='SHIP' and  wo_id_fk ='" + wo_id + "'";
		List<Ret_box> retBoxList = retBoxDao.find(hql);
		for (Ret_box ret_box : retBoxList) {
			wip_qty = wip_qty + comObj2Long(ret_box.getPrd_qty());
		}
		pln_prd_qty = comObj2Long(ret_wo.getPln_prd_qty());
		sale_qty = pln_prd_qty - comObj2Long(ret_wo.getWh_in_prd_qty());

		oaryB.setPln_prd_qty(pln_prd_qty);
		oaryB.setSale_qty(sale_qty);
		oaryB.setWip_qty(wip_qty);
		oaryB.setWh_in_qty(comObj2Long(ret_wo.getWh_in_prd_qty()) - wip_qty);

		List<XpaplywoOB> oaryBList = new ArrayList<XpaplywoOB>();
		oaryBList.add(oaryB);
		xpaplywoO.setOaryB(oaryBList);
		xpaplywoO.setTbl_cnt(1);
		xpaplywoO.setRtn_code(RETURN_CODE_OK);
		xpaplywoO.setRtn_mesg(RETURN_MESG_OK);
		return xpaplywoO;
	}

	private void closeWOProc() {
		String soID;
		Timestamp currentTimeStamp = Util.getCurrentTimestamp();
		Ret_wo ret_wo = null;
		try {
			List<XpaplywoIA> iaryList = inTrx.getIary();
			for (XpaplywoIA iary : iaryList) {
				soID = iary.getWo_id();
				ret_wo = retWODao.get(soID);
				ret_wo.setWo_stat(WO_STATUS_CLOS);
				ret_wo.setEvt_usr(iary.getEvt_user());
				ret_wo.setEvt_timestamp(currentTimeStamp);
				ret_wo.setAct_cmp_timestamp(currentTimeStamp);
				retWODao.update(ret_wo);
			}
		} catch (Exception e) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(Util.stackTraceToString(e));
			return;
		}
		getWoProc();
	}

	private void openWOProc() {

		String soID;
		Ret_wo ret_so = null;
		try {
			List<XpaplywoIA> iaryList = inTrx.getIary();
			for (XpaplywoIA iary : iaryList) {
				soID = iary.getWo_id();
				ret_so = retWODao.get(soID);
				ret_so.setWo_stat(WO_STATUS_WAIT);//TODO 是INPR还是WAIT
				ret_so.setEvt_usr(iary.getEvt_user());
				ret_so.setEvt_timestamp(Util.getCurrentTimestamp());
				retWODao.update(ret_so);
			}
		} catch (Exception e) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(Util.stackTraceToString(e));
			return;
		}
		getWoProc();

	}

	public void errActionFnc() {
		outTrx.setTrx_id("XPAPLYWO");
		outTrx.setType_id("O");
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}

	public long comObj2Long(Object obj) {
		if (obj != null && obj.equals("")) {
			return Long.valueOf(obj.toString());
		}
		return 0;
	}

	public int comObj2Int(Object obj) {
		if (obj != null) {
			return Integer.valueOf(obj.toString());
		}
		return 0;
	}

	private String getStatDsc(String woStat) {
		if (woStat.equals("CLOS")) {
			return "关闭";
		} else if (woStat.equals("WAIT")) {
			return "等待";
		} else if (woStat.equals("INPR")) {
			return "制程中";
		} else if (woStat.equals("HOLD")) {
			return "锁定";
		}
		return "其他";
	}

	public void getWoBindSoProc() {
		outTrx.setTrx_id("XPAPLYWO");
		outTrx.setType_id("O");
		try {
			String sql = "FROM Ret_wo where wo_typ='W'  AND wo_stat='WAIT' ORDER BY WO_ID DESC";
			List<Ret_wo> woList = retWODao.find(sql);
			List<XpaplywoOA> oaryList = new ArrayList<XpaplywoOA>();
			if (woList != null) {
				for (Ret_wo ret_wo : woList) {
					if (!Util.isSpaceCheck(ret_wo.getSo_id())) {
						XpaplywoOA oary = new XpaplywoOA();
						oary.setWo_id(ret_wo.getWo_id());
						oary.setSo_id(ret_wo.getSo_id());
						oaryList.add(oary);
					}
				}
			}
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setRtn_mesg(RETURN_MESG_OK);
			outTrx.setOary(oaryList);
			outTrx.setTbl_cnt(oaryList.size());
		} catch (Exception ex) {
			outTrx.setRtn_code(E_XPAPLYWO_OTHER_ERROR + "");
			outTrx.setRtn_mesg(ex.getMessage());
			logger.error(Util.stackTraceToString(ex));
			return;
		}
	}
}
